#!/bin/bash

set -eo pipefail

pwd

source main-var.sh
source ops-sys.sh
source ops-mvn.sh
source ops-git.sh
source ops-deploy.sh
source ops-docker.sh
source ops-api.sh


export buildAt="$(date '+%Y-%m-%d %H:%M:%S')"

function print-report(){
  echo "﹍﹎﹍﹎﹍﹎﹎﹎"
  echo "产品线: $productLine_name ($productLine_code)"
  echo "开发语言: $language:$languageVersion"
  echo "K8s服务名: $serviceName"
  echo "git分支: $gitBranch"
  echo "构建文件夹: $folder"
  echo "注册中心: $msDeployEnvEnum"
  echo "注册中文名: 中台api"
  echo "注册名: $appCnName"
  echo "运行环境: $msDeployEnvEnum - $profile"
  echo "集群名: $clusterCode"
  echo "docker镜像: $serviceName:$dockerImageVersion"
  echo "git仓库: $gitUrl"
  echo "git提交id: $gitCommitId"
  echo "git提交时间: $gitCommitAt"
  echo "git提交者: $gitCommitAuthor"
  echo "生成时间: $buildAt"
  echo "﹊﹉﹊﹉﹊﹉﹊﹊"
}

function run-in-docker(){
  cmd=$1
  if [[ "$cmd" = *".sh" ]]; then
    cmd=" source $cmd"
  fi

  env_timezone=
  if [ -n "$env_timezone" ]; then
      TZ="$env_timezone"
      env_timezone=" -e TZ=$env_timezone "
  fi

  echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "${sudo} docker $env_bashImageName 执行 $cmd"

  ${sudo} docker run --rm --privileged=true  -e TZ="Asia/Shanghai" $env_dockerBuildAddHostsParameters \
      -v  "$(getPwdInDockerPath)":"$(pwd)" -w "$(pwd)"  \
      -v  $env_jenkinsHomePathMountToDocker:$env_jenkinsHomePathMountToDocker \
      $env_bashImageName bash -c "source main.sh &&  $cmd "
}

function sys-init(){
  echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "执行初始化"

  if [ "$language" == "java" ]; then
    mkdir -p res/$folder/lib-runtime

    mkdir -p /data/mvn-repo
  fi

  mkdir -p res/$folder/res-runtime

  print-report > res/$folder/res-runtime/ops-report.txt


  echo "项目中的文件夹："
  echo "$folder/res-runtime：在编译时，会把它移到上层，并参与编译！"

  cat res/$folder/res-runtime/ops-report.txt
  cp -f main-var.sh res/
  cp -f main-var.sh res/$folder/

}



function show-path(){
  echo "`date "+%Y-%m-%d %H:%M:%S"` `pwd` 文件夹结构: "
  tree -L 3 -a || ( ls -A &&  echo "`pwd`/code 文件夹结构: " && ls -A code )
}
